<!--
 * @Description: 通用商品选择弹窗
 * @Author: Li Yujie
 * @Date: 2020/12/18
 * @Last Modified by: Li Yujie
 * @Last Modified time: 2020/12/18
-->
<template>
    <div
        class="goods-select-dialog"
        v-if="dialogVisible"
    >
        <el-dialog
            :title="title"
            :visible.sync="dialogVisible"
            width="1250px"
            :close-on-click-modal="false"
        >
            <div class="wrap">
                <div class="filter-view">
                    <div class="filter-item">
                        <div class="filter-item__label">商品编号</div>
                        <el-input
                            placeholder="请输入商品编号"
                            v-model="formData.goods_nos"
                            clearable
                            size="medium"
                            style="width: 188px"
                            @keydown.enter.native="getTableData(1)"
                        />
                    </div>
                    <div class="filter-item">
                        <div class="filter-item__label">商品名称</div>
                        <el-input
                            placeholder="请输入商品名称"
                            v-model="formData.keyword"
                            clearable
                            size="medium"
                            style="width: 188px"
                            @keydown.enter.native="getTableData(1)"
                        />
                    </div>
                    <div class="filter-item">
                        <div class="filter-item__label">店铺名称</div>
                        <el-input
                            placeholder="请输入店铺名称"
                            v-model="formData.shop_title"
                            clearable
                            size="medium"
                            style="width: 188px"
                            @keydown.enter.native="getTableData(1)"
                        />
                    </div>
                    <div class="filter-item">
                        <div class="filter-item__label">公司名称</div>
                        <el-input
                            placeholder="请输入公司名称"
                            v-model="formData.company_name"
                            clearable
                            size="medium"
                            style="width: 188px"
                            @keydown.enter.native="getTableData(1)"
                        />
                    </div>
                    <div
                        class="filter-item"
                        v-if="!(fromType === 'advent_goods' || fromType === 'group_goods')"
                    >
                        <div class="filter-item__label">商品类型</div>
                        <el-select
                            size="medium"
                            v-model="formData.goodsType"
                            placeholder="商品类型"
                            clearable
                            style="width: 188px"
                            @change="getTableData(1)"
                        >
                            <el-option
                                v-for="item in goodsTypeList"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value"
                            />
                        </el-select>
                    </div>
                    <div class="filter-item">
                        <div class="filter-item__label">商品分类</div>
                        <el-cascader
                            placeholder="商品分类"
                            :options="classifyList"
                            v-model="formData.type_no"
                            filterable
                            clearable
                            change-on-select
                            style="width: 188px"
                            expand-trigger="click"
                            size="medium"
                            @change="getTableData(1)"
                            :props="{
                                label: 'title',
                                value: 'no',
                                children: 'children'
                            }"
                        />
                    </div>
                    <div class="filter-item">
                        <el-checkbox
                            v-model="formData.status"
                            v-if="fromType !== 'advent_goods'"
                            @change="getTableData(1)"
                        >只显示已上架
                        </el-checkbox>
                    </div>
                    <div class="filter-item">
                        <el-button
                            type="text"
                            size="medium"
                            @click="goShopList()"
                        >
                            商品管理
                        </el-button>
                        <el-button
                            type="text"
                            size="medium"
                            @click="refresh()"
                        >
                            刷新
                        </el-button>
                    </div>
                </div>
                <el-table
                    :data="tableData"
                    height="100%"
                    style="width: 100%;"
                    ref="goodsTable"
                    class="tableDate"
                    v-loading="loading"
                    row-key="goods_no"
                    @selection-change="handleSelectionChange"
                >
                    <el-table-column
                        type="selection"
                        align="center"
                        :select="true"
                        :reserve-selection="true"
                        width="50"
                        v-if="dialogType === 2 || dialogType === 3"
                    />
                    <el-table-column
                        prop="goods_cover"
                        label="商品信息"
                        :formatter="emptyFormatter"
                        align="left"
                        width="180"
                    >
                        <template slot-scope="scope">
                            <div class="info">
                                <img
                                    :src="scope.row.goods_cover"
                                    :style="{width: '64px', height: '64px', marginRight: '12px'}"
                                    @click="fnShowPreview(scope.row.goods_cover)"
                                >
                                <el-tooltip
                                    effect="dark"
                                    :content="scope.row.goods_title"
                                    placement="top">
                                    <div class="info_main">
                                        {{ scope.row.goods_title }}
                                    </div>
                                </el-tooltip>
                            </div>
                        </template>
                    </el-table-column>
                    <el-table-column
                        prop="goods_no"
                        label="商品编号"
                        show-overflow-tooltip
                        :formatter="emptyFormatter"
                    />
                    <el-table-column
                        prop="goods_type"
                        label="商品类型"
                        show-overflow-tooltip
                        :formatter="emptyFormatter"
                        align="center"
                    >
                        <template slot-scope="scope">
                            <p v-if="scope.row.goods_type === 1">自营</p>
                            <p v-else-if="scope.row.goods_type === 3">秒杀</p>
                            <p v-else-if="scope.row.goods_type === 5">折扣专区</p>
                            <p v-else-if="scope.row.goods_type === 6">0元购</p>
                            <p v-else-if="scope.row.goods_type === 7">砍价</p>
                            <p v-else-if="scope.row.goods_type === 8">虚拟商品</p>
                            <p v-else-if="scope.row.goods_type === 2">淘宝</p>
                            <p v-else-if="scope.row.goods_type === 11">京东</p>
                            <p v-else-if="scope.row.goods_type === 12">拼多多</p>
                            <p v-else>--</p>
                        </template>
                    </el-table-column>
                    <el-table-column
                        prop="type_name"
                        label="商品分类"
                        show-overflow-tooltip
                        :formatter="emptyFormatter"
                        align="center"
                    />
                    <el-table-column
                        prop="shop_title"
                        label="店铺名称"
                        show-overflow-tooltip
                        :formatter="emptyFormatter"
                    />
                    <el-table-column
                        prop="goods_price"
                        label="售价"
                        show-overflow-tooltip
                        :formatter="emptyFormatter"
                        align="center"
                    />
                    <el-table-column
                        label="一卡通折扣"
                        show-overflow-tooltip
                        :formatter="emptyFormatter"
                        align="center"
                        v-if="fromType === 'advent_goods'"
                    >
                        <template slot-scope="scope">
                            <div v-if="scope.row.has_vip_discount">
                                {{ scope.row.goods_discount_vip }}折
                            </div>
                            <div v-else>--</div>
                        </template>
                    </el-table-column>
                    <el-table-column
                        prop="tuan_price"
                        label="团购价"
                        show-overflow-tooltip
                        :formatter="emptyFormatter"
                        align="center"
                        v-if="fromType === 'advent_goods'"
                    />
                    <el-table-column
                        prop="goods_price_origin"
                        label="原价"
                        show-overflow-tooltip
                        :formatter="emptyFormatter"
                        align="center"
                        v-if="fromType !== 'advent_goods'"
                    />
                    <el-table-column
                        prop="goods_stock"
                        label="库存"
                        show-overflow-tooltip
                        align="center"
                    />
                    <el-table-column
                        prop="goods_sale"
                        label="真实销量"
                        show-overflow-tooltip
                        align="center"
                    />
                    <el-table-column
                        label="其他活动优惠"
                        :formatter="emptyFormatter"
                        align="center"
                        width="180"
                    >
                        <template slot-scope="scope">
                            <div v-if="scope.row.benefit_list.length">
                                <div
                                    v-for="(item,index) in scope.row.benefit_list"
                                    :key="index"
                                >
                                    {{ item.info }}
                                </div>
                            </div>
                            <div v-else>--</div>
                        </template>
                    </el-table-column>
                    <el-table-column
                        label="操作"
                        show-overflow-tooltip
                        align="center"
                        v-if="dialogType === 1"
                    >
                        <template slot-scope="scope">
                            <el-button
                                type="primary"
                                size="small"
                                @click="handleSelectGoods(scope.row)"
                            >
                                选择
                            </el-button>
                        </template>
                    </el-table-column>
                </el-table>

                <!-- /分页 -->
                <div class="pagination-wrapper">
                    <el-pagination
                        background
                        layout="total, prev, pager, next, jumper"
                        :current-page.sync="page.now_page"
                        :total="page.total_count"
                        :page-size="page.page_size"
                        @current-change="getTableData"
                    />
                </div>
            </div>
            <span
                slot="footer"
                class="dialog-footer"
                v-if="dialogType === 1"
            >
                <el-button
                    @click="dialogVisible = false"
                    size="medium"
                    :style="{width: '126px', marginRight: '52px'}"
                >
                    关 闭
                </el-button>
            </span>
            <span
                slot="footer"
                class="dialog-footer"
                v-if="dialogType === 2"
            >
                <el-button
                    @click="dialogVisible = false"
                    size="medium"
                    :style="{width: '126px', marginRight: '52px'}"
                >
                    取 消
                </el-button>
                <el-button
                    type="primary"
                    :style="{width: '126px'}"
                    @click="confirm()"
                >
                    确 定
                </el-button>
            </span>
            <span
                slot="footer"
                class="dialog-footer"
                v-if="dialogType === 3"
            >
                <el-button
                    type="primary"
                    :style="{width: '126px'}"
                    @click="confirm()"
                >
                    添加到商品组
                </el-button>
            </span>
        </el-dialog>

        <!-- 图片预览 -->
        <img-preview ref="preview"/>
    </div>
</template>

<script>
import imgPreview from '@/components/common/img-preview';

export default {
    name: 'GoodsSelect',
    props: {
        title: {
            type: String,
            default: '选择商品'
        },
        // 使用来源 advent_goods临期商品 group_goods社群商品
        fromType: {
            type: String,
            default: ''
        },
        // 额外请求参数
        params: {
            type: Object,
            default: () => {
                return {};
            }
        }
    },
    components: {
        imgPreview
    },
    data() {
        return {
            loading: true,
            // 1单选 2 多选
            dialogType: 1,
            dialogVisible: false,
            tableData: [],//表格数据
            page: {
                now_page: 1,
                total_count: 0,
                page_size: 0
            },
            // changeData: {},//选择数据
            formData: {
                // 上架状态
                status: true,
                // 商品名称或编号
                keyword: '',
                goodsType: '',
                type_no: '',
                shop_no: '',
                goods_nos: '',
                shop_title: '',
                company_name: '',
            },
            multipleSelection: [],
            goodsTypeList: [
                {label: '自营', value: 1},
                {label: '折扣', value: 5},
                {label: '0元购', value: 6}
            ],
            requestUrl: '/student/shop_goods%5Cget_list_for_select',
            classifyList: []
        };
    },
    watch: {},
    mounted() {
        this.getClassifyList();
    },
    methods: {
        /**
         * @description 显示弹窗
         * @param type - 1单选 2 多选
         * @param requestUrl - 接口地址
         * @param typeList - 商品类型列表
         * @param goods_type - 筛选商品类型
         */
        show(type = 1 ,requestUrl = this.requestUrl, typeList = this.goodsTypeList, goods_type = '') {
            // eslint-disable-next-line no-param-reassign
            if(requestUrl === '') requestUrl = this.requestUrl;
            this.dialogType = type;
            this.goodsTypeList = typeList;
            this.formData.goodsType = goods_type;
            this.requestUrl = requestUrl;
            this.dialogVisible = true;
            this.getTableData(1);
        },

        /**
         * @description 编辑商品
         *  list: 选择商品
         */
        edit(type = 1, list, requestUrl = this.requestUrl, typeList = this.goodsTypeList) {
            this.dialogType = type;
            this.goodsTypeList = typeList;
            this.requestUrl = requestUrl;
            this.dialogVisible = true;
            this.getTableData(1);

            if (!list.length) return;
            setTimeout(() => {
                this.multipleSelection = list;
                this.multipleSelection.forEach(el => {
                    this.$refs.goodsTable.toggleRowSelection(el, true);
                });
            }, 200);
        },

        /**
         * @description 修改数据回填
         */
        setData (list) {
            this.toggleRowSelect(JSON.parse(JSON.stringify(list)));
            this.refresh();
        },

        toggleRowSelect(rows) {
            const _this = this;

            if (rows.length > 0) {
                this.$nextTick(() => {
                    rows.forEach(row => {
                        _this.$refs.goodsTable.toggleRowSelection(row, true);
                    });
                });
            } else {
                this.$refs.goodsTable.clearSelection();
            }
        },

        /**
         * @description 关闭弹窗
         */
        close() {
            this.dialogVisible = false;
        },

        /**
         * @description 获取商品数据
         */
        getTableData(page) {

            this.loading = true;
            let type_no = '';

            if (this.formData.type_no.length !== 0) {
                type_no = this.formData.type_no[this.formData.type_no.length - 1];
            }
            this.$post(this.requestUrl, {
                page_id: page || this.page.now_page,
                only_sale: this.formData.status ? 1 : 0,
                keyword: this.formData.keyword,
                goods_type: this.formData.goodsType,
                type_no: type_no,
                shop_no: this.formData.shop_no,
                goods_nos: this.formData.goods_nos,
                shop_title: this.formData.shop_title,
                company_name: this.formData.company_name,
                page_size: 20,
                ...this.params
            }, resp => {
                if (resp.code === 1) {
                    if (Object.prototype.toString.call(resp.data) == '[object Null]') return false;
                    this.tableData = resp.data.list;
                    this.page.now_page = resp.data.page.now_page;
                    this.page.total_count = resp.data.page.total_count;
                    this.page.page_size = resp.data.page.page_size;
                }
                this.loading = false;
            });
        },

        /**
         * @description 刷新
         */
        refresh() {
            this.loading = true;
            this.checkedAll = false;
            this.getTableData();
        },

        /**
         * @description 查看图片
         * @param   {String}    images    单图预览
         * @param   {Array}     images     多图预览
         */
        fnShowPreview(images) {
            if (images && Object.prototype.toString.call(images) == '[object String]') {
                this.$refs.preview.show([
                    images
                ]);
            } else if (images && Object.prototype.toString.call(images) == '[object Array]') {
                this.$refs.preview.show(images);
            } else {
                this.$notify({
                    title: '提示',
                    message: '图片类型错误',
                    type: 'warning',
                    duration: 2000
                });
            }
        },

        /**
         * @description 商品管理
         */
        goShopList() {
            let routeData = this.$router.resolve({
                name: 'GoodsList'
            });

            window.open(routeData.href, '_blank');
        },

        /**
         * @description 过滤函数
         */
        emptyFormatter(row, column, val) {
            if (!val) return '--';
            return val;
        },

        /**
         * 批量确定提交
         */
        confirm() {
            if (this.multipleSelection.length === 0) {
                this.$message.warning('请先选择商品');
                return;
            }
            this.$emit('change', this.multipleSelection);
            this.close();
        },

        /**
         * 单个商品选择确定
         * @param goods
         */
        handleSelectGoods(goods) {
            this.$emit('change', goods);
            this.close();
        },

        /**
         * 表格多选事件
         * @param val
         */
        handleSelectionChange(val) {
            this.multipleSelection = val;
        },

        /**
         * 获取商品分类列表
         */
        getClassifyList() {
            this.$post('/student/shop_goods_type%5Cget_list_linkage', {}, resp => {
                if (resp.code === 1) {
                    this.classifyList = resp.data.list;
                }
            });
        }
    }
};
</script>

<style lang="less" scoped>
.goods-select-dialog {
    .dialog-footer {
        display: flex;
        align-items: center;
        justify-content: center;
    }

    .wrap {
        display: flex;
        flex-direction: column;
        box-sizing: border-box;
        height: 585px;
        .filter-view {
            display: flex;
            flex-wrap: wrap;
            align-items: center;
            .filter-item {
                margin-right: 24px;
                margin-bottom: 24px;
                display: flex;
                align-items: center;
                &__label {
                    margin-right: 12px;
                }
            }
        }

        .tableDate {
            .info {
                display: flex;
                align-items: center;
            }

            .info_main {
                overflow: hidden;
                text-overflow: ellipsis;
                display: -webkit-box;
                -webkit-line-clamp: 2;
                /*! autoprefixer: off */
                -webkit-box-orient: vertical;
            }
            ::-webkit-scrollbar {
                width: 6px;
                height: 6px;
            }
        }

        /* 分页 */

        .pagination-wrapper {
            background-color: #fbfbfb;
            box-sizing: border-box;
            border: 1px #EBEEF5 solid;
            border-top: 0;
            border-radius: 0 0 4px 4px;
            padding: 14px 24px;

            .el-pagination {
                float: right;
                padding: 0;
                background-color: transparent;
            }
        }
    }
    /deep/ .el-dialog__header {
        padding-top: 10px;
        box-shadow:0 1px 0 0 rgba(233,233,233,1);
        display: flex;
        align-items: center;
        background: #FAFAFA;
        border-radius: 4px 4px 0 0;
        .el-dialog__title {
            font-weight: 500;
            color: #333;
            font-size: 14px;
        }
        .el-icon-close:before {
            color: rgb(194,194,194);
            font-size: 17px;
            font-weight: bold;
        }
        .el-dialog__headerbtn {
            top: 14px;
        }
    }
    /deep/ .el-dialog__body {
        padding: 24px 32px;
    }
    /deep/ .el-dialog__footer {
        padding: 0 0 32px;
    }
}
/deep/ .el-dialog {
    margin-top: 7vh!important;
}

/deep/ .el-table {
    background: #FBFBFB;
    border-radius: 4px;
    border: 1px solid #EEEEEE;
    border-bottom: 0;
    th {
        background: #f2f2f2!important;
        font-size: 14px!important;
        font-family: PingFangSC-Medium, PingFang SC!important;
        font-weight: 500!important;
        color: #333333!important;
        height: 44px!important;
    }
}

</style>
